package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.*;
import com.monitor.entity.*;
import com.monitor.service.ErpClaimsService;
import com.monitor.util.BlankUtil;
import com.monitor.util.IDUtils;
import com.monitor.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 理赔单表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-25
 */
@Service
public class ErpClaimsServiceImpl extends ServiceImpl<ErpClaimsMapper, ErpClaims> implements ErpClaimsService {

	@Resource
	private ErpClaimsMapper erpClaimsMapper;
	
	@Resource
	private ErpMaterielMapper erpMaterielMapper;
	
	@Resource
	private ErpCustomerCarMapper erpCustomerCarMapper;
	
	@Resource
	private ErpUserMapper erpUserMapper;
	
	@Resource
	private ErpCustomerMapper erpCustomerMapper;
	
	@Resource
	private ErpMaterielFlowMaterielMapper erpMaterielFlowMaterielMapper;
	
	@Resource
	private ErpManagementMapper erpManagementMapper;
	
	@Override
	public Result<List<ErpClaims>> selectByCustName(int type,String names) {
		// TODO Auto-generated method stub
		List<ErpClaims> list=erpClaimsMapper.selectByCustName(type,names);
		return new Result<List<ErpClaims>>(list, list.size());
	}

	@Override
	public Result<Integer> addClaims(ErpClaims claims,Integer custId) {
		// TODO Auto-generated method stub
		String id="ZLLP"+IDUtils.genOrderId();
		if(claims.getType()==2) {
			id="WLSP"+IDUtils.genOrderId();
		}
    	List<ErpClaims> list=erpClaimsMapper.selectList(new EntityWrapper<ErpClaims>().like("claims_id", id).orderBy("id", false));
    	if(list.size()>0) {
    		ErpClaims cla=list.get(0);
    		String str=cla.getClaimsId().substring(12, cla.getClaimsId().length());
    		str=IDUtils.addOne(str);
    		id+=str;
    	}else {
    		id+="00001";
    	}
    	claims.setClaimsId(id);
    	ErpUser user=erpUserMapper.selectById(custId);
        
        List<ErpMaterielFlowMateriel> flowMaterielList= erpMaterielFlowMaterielMapper.selectList(new EntityWrapper<ErpMaterielFlowMateriel>().eq("materiel_name",claims.getMName()).eq("materiel_brand",claims.getBrand()).eq("materiel_Specifications",claims.getSpecification()).eq("materiel_model",claims.getSize()).eq("materiel_unit",claims.getClaimsUnit()));
        if(flowMaterielList.size()>0) {
        	claims.setMaterielId(flowMaterielList.get(0).getId());
        }
        ErpManagement management=new ErpManagement();
        ErpCustomer customer=erpCustomerMapper.selectById(user.getCustomerId());
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
        management.setDt(dateString);
        management.setWorkId(id);
        management.setClientName(claims.getCustomerName());
        management.setCarNumber(claims.getCarPalte());
        management.setWorkProject("质量理赔");
        if(claims.getType()==2) {
        	management.setWorkProject("物料索赔");
		}
        management.setMaintian(claims.getClaimsPrice());
        List<ErpCustomer> custList=erpCustomerMapper.selectList(new EntityWrapper<ErpCustomer>().eq("cus_name",claims.getCustomerName()));
        if(custList.size()>0) {
        	management.setCustomerId(custList.get(0).getId());
        }
        if(claims.getType()==1){
            management.setCope(claims.getClaimsPrice());
            //此处应该生成付款单
        }
        if(claims.getType()==2){
            management.setAccounts(claims.getClaimsPrice());
            //此处应该生成收款单
        }
        management.setWorkPlace(customer.getCusName());
        erpManagementMapper.insert(management);

		int count=erpClaimsMapper.insert(claims);
		return new Result<Integer>(count, 1);
	}

	@Override
	public Result<List<Map<String, Object>>> selectSaleAndOrderList(int custId, String word) {
		// TODO Auto-generated method stub
		word="%"+word+"%";
		List<Map<String, Object>> list=erpClaimsMapper.selectSaleAndOrderList(custId, word);
		return new Result<List<Map<String,Object>>>(list, list.size());
	}

	@Override
	public Result<Map<String, Object>> selectDateilBySrsId(int srId) {
		// TODO Auto-generated method stub
		try {
			Map<String, Object> map=erpClaimsMapper.selectDateilBySrsId(srId);
			String materiel_production_id=map.get("materiel_production_id").toString();
			ErpMateriel materiel=erpMaterielMapper.selectByProductionId(materiel_production_id);
			map.put("materiel_dot", materiel.getMaterielDot());
			String outTo=map.get("materiel_out_to").toString();
			String[] che=outTo.split("-");
			String plate=che[0];
			ErpCustomerCar car=erpCustomerCarMapper.selectByPlate(plate);
			map.put("car_type", car.getCarType1()+car.getCarType2());
			return new Result<Map<String,Object>>(map, 1);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<Map<String,Object>>(1, e.getMessage());
		}
	}

	@Override
	public Result<List<Map<String, Object>>> selectSaleAndOrderListByCarPlate(int custId, String word) {
		// TODO Auto-generated method stub
		List<String> list=erpCustomerCarMapper.selectPlateByCustId(custId);
		String plates=BlankUtil.listStringToString(list);
		word="%"+word+"%";
		List<Map<String, Object>> list1=erpClaimsMapper.selectSaleAndOrderListByCarPlate(plates, word);
		return new Result<List<Map<String,Object>>>(list1, list1.size());
	}
	
	

}
